package structure;

import java.util.ArrayList;

import algorithm.miscellaneous;

public class bigProduction {
	
	private ArrayList<String> items;//da togliere?
	private ArrayList<String> rightside;
	private String leftside;
	private ArrayList<String[]> connection;
	
	private static String dollar= "$";
	private static String dot= ".";
	
	//costruttori
	public bigProduction(){

		this.items= new ArrayList<String>();
		this.rightside= new ArrayList<String>();
		this.leftside= new String();
		this.connection= new ArrayList<String[]>();
	}
	
	public bigProduction(enumerateGrammar e, int position){
		
		this.items= new ArrayList<String>();
		this.rightside= new ArrayList<String>();
		this.leftside= e.getLeftside().get(position);
		this.connection= new ArrayList<String[]>();
		
		if(position <= e.getEnumeratedGrammar().size()){
			
			this.createItems(e, position);
			
			this.connectItem();
			
		}
		else{
			System.out.println("Inserita una posizione non presente nella grammatica");
		}
	}
	//
	
	//crea i vari item
	private void createItems(enumerateGrammar e, int position){
		
		String leftside= e.getLeftside().get(position);
		String rightside= e.getRightside().get(position);
		char[] rightchar= rightside.toCharArray();
		char dot= this.dot.charAt(0);
		
		int length= rightside.length();
		
		if(length == 1 && rightchar[0] == 'e'){
			
			String tmpItem= leftside + "->" + this.dot;
			this.items.add(tmpItem);
			this.rightside.add(this.dot);
			
		}
		else{
			
			for(int i=0; i<length+1; i++){
				
				char[] tmpC= new char[length+1];
				//caso con il punto all'inizio
				if(i == 0){
					int j=0;
					tmpC[j]= dot;
					for(char c : rightchar){
						j++;
						tmpC[j]= c;
					}
				}
				//caso delle combinazioni in mezzo
				else if(i>0 && i<length+1){
					
					for(int j=0; j<i; j++){
						tmpC[j]= rightchar[j];
					}
					
					tmpC[i]= dot;
					
					for(int j=i; j<length; j++){
						tmpC[j+1]= rightchar[j];
					}
				}
				//caso con il punto alla fine
				else if(i == length){
					
					for(int j=0; j<length; j++){
						tmpC[j]= rightchar[j];
					}
					tmpC[i]= dot;
				}
				
				String tmpS= new String(tmpC);
				String tmpItem= leftside + "->" + tmpS;
				this.items.add(tmpItem);
				this.rightside.add(tmpS);
			}
		}
		
	}
	
	//creo le connessioni tra i vari item della stessa big production
	private void connectItem(){
		
		
		for(int i=0; i<this.rightside.size(); i++){
			
			String[] tmpArS= new String[3];
			
			String s= this.rightside.get(i);
			//prendo il simbolo dopo il .
			String tmpS= miscellaneous.getSymbolAfterDot(s);
			if(!tmpS.equals("")){
				
				tmpArS[0]= s;
				tmpArS[1]= tmpS;
				tmpArS[2]= this.rightside.get(i+1);
				
				this.connection.add(tmpArS);
			}
			else{
				
				tmpArS[0]= s;
				tmpArS[1]= "";
				tmpArS[2]= "";
				
				this.connection.add(tmpArS);
			}
		}
		
	}
	
	//get
	
	public String getLeftside(){
		
		return this.leftside;
	}
	
	public ArrayList<String> getRightside(){
		
		return this.rightside;
	}
	
	public ArrayList<String[]> getConnection(){
		
		return this.connection;
	}
	//
	public void printItAll(){
		
		/*System.out.println("La dimensione di rightside è= " + this.rightside.size());
		
		System.out.println("Rightside= " + this.rightside);
		
		System.out.println("La dimensione di connection è= " + this.connection.size());*/
		
		if(this.connection.size() > 1){
		
			for(String[] as : this.connection){
				
				System.out.print("[" + this.leftside + "->" + as[0] +"] --->" + as[1] + " ");
			}
			String g= this.connection.get(this.connection.size()-1)[2];
			System.out.println("[" + this.leftside + "->" + g + "]");
		}
		else if(this.connection.size() == 1){
			
			System.out.println("[" + this.leftside + "->" + this.connection.get(0)[0] +"]");
		}
	}

}
